package face.pay.web.enterprise.controller.baseInfo;

import face.pay.common.enterprise.service.IBaseMchInfoService;
import face.pay.entity.BaseMchInfo;
import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.util.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Auther: cjj
 * @Date: 2019/9/18
 * @Description:
 */
@Slf4j
@Controller
@RequestMapping("/baseInfo")
public class BaseInfoController {

    @Autowired
    private IBaseMchInfoService baseMchInfoService;

    @RequestMapping("/index")
    public String manager(Model model, @CurrentUser UserInfo loginUser) {
        //根据当前商户登录的id商户的基本信息
        BaseMchInfo baseMchInfo = baseMchInfoService.queryBaseMachInfoByUserId(loginUser);
        model.addAttribute("baseMchInfo", baseMchInfo);
        return "enterprise/base_mch_info";
    }

    /**
     * 文件上传
     *
     * @param file
     * @param loginUser
     * @return
     */
    @RequestMapping("/fielUpload")
    @ResponseBody
    public ResultMessage fielUpload(@RequestParam("file") MultipartFile file, @CurrentUser UserInfo loginUser) throws Exception {
        log.info("上传开始");
        byte[] fileBytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        return this.baseMchInfoService.fielUpload(fileBytes, originalFilename, loginUser);
    }

    /**
     * 获取文件
     *
     * @param loginUser
     * @param fielId
     * @return
     */
    @RequestMapping(value = "/getFiel", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] getFiel(@CurrentUser UserInfo loginUser, @RequestParam String fielId) {
        return baseMchInfoService.getMedia(fielId, loginUser);
    }

    /**
     * 商户基本资料编辑
     *
     * @param
     * @param loginUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit")
    public ResultMessage subinfo(BaseMchInfo baseMchInfo, @CurrentUser UserInfo loginUser)  {
        return this.baseMchInfoService.edit(baseMchInfo, loginUser);
    }
}
